package com.iraeta.service.etl;

import cn.alibaba.open.param.AlibabaCaigouProductAddOrUpdateErpProductCoopParam;
import cn.alibaba.open.param.AlibabaCaigouProductBatchAddOrUpdateErpProductsParam;
import cn.alibaba.open.param.AlibabaCaigouProductBatchAddOrUpdateErpProductsResult;
import cn.hutool.core.date.DateUtil;
import com.alibaba.ocean.rawsdk.ApiExecutor;
import com.iraeta.Config;
import com.iraeta.common.ServiceException;
import com.iraeta.dao.erp.WuLiaoDao;
import com.iraeta.dao.main.BusslogDao;
import com.iraeta.dao.main.MyconfigDao;
import com.iraeta.entity.main.Busslog;
import com.iraeta.entity.main.Myconfig;
import com.iraeta.uitl.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class WuLiaoTongBuService {
    private static final Logger logger = LoggerFactory.getLogger(WuLiaoTongBuService.class);
    @Autowired
    Config config;
    @Autowired
    WuLiaoDao wuLiaoDao;
    @Autowired
    MyconfigDao myconfigDao;
    @Autowired
    BusslogDao busslogDao;

    public void WuLiaoTongBu(){
        try {
            String tongbuwuliao = myconfigDao.selectByKey("tongbuwuliao").getItemvalue();
            if(!tongbuwuliao.equals("1")){
                return;
            }
            String now = DateUtil.now();
            Myconfig myconfig = myconfigDao.selectByKey("last_updatetime");
            String lastupdatetime = myconfig.getItemvalue();
            int groupSize= 15;
            List<HashMap<String, Object>> needTongBuWuLiaoList = wuLiaoDao.listNeedTongBuData(lastupdatetime);
            logger.info(now + "开始同步" + needTongBuWuLiaoList.size() +"条数据");
            for(int i = 0; i<needTongBuWuLiaoList.size(); i+=groupSize){
                List<HashMap<String, Object>> subList = needTongBuWuLiaoList.subList(i, Math.min(i+groupSize, needTongBuWuLiaoList.size()));
                wuLiaoTo1688(subList);
            }
            myconfig.setItemvalue(now);
            myconfigDao.updateByPrimaryKey(myconfig);
            Busslog busslog = new Busslog("wuliaotongbu", "成功", "同步了" + needTongBuWuLiaoList.size() + "条料品信息");
            busslogDao.insert(busslog);
        }catch (Exception e){
            logger.error("WuLiaoTongBu_exception:" + e.getMessage());
            Busslog busslog = new Busslog("wuliaotongbu", "失败", e.getMessage());
            busslogDao.insert(busslog);
        }
    }

    private void wuLiaoTo1688(List<HashMap<String, Object>> subList){
        Myconfig myconfig = myconfigDao.selectByKey("wuliao_prefix");
        String wuliao_prefix = myconfig.getItemvalue();

        ApiExecutor apiExecutor = new ApiExecutor(config.serverHost, Integer.parseInt(config.httpPort), Integer.parseInt(config.httpsPort), config.appKey, config.appSercret);
        AlibabaCaigouProductBatchAddOrUpdateErpProductsParam param = new AlibabaCaigouProductBatchAddOrUpdateErpProductsParam();
        AlibabaCaigouProductAddOrUpdateErpProductCoopParam[] coopParams = new AlibabaCaigouProductAddOrUpdateErpProductCoopParam[subList.size()];
        for(int i = 0; i<subList.size(); i++){
            HashMap<String, Object> itemMap = subList.get(i);
            AlibabaCaigouProductAddOrUpdateErpProductCoopParam cp = new AlibabaCaigouProductAddOrUpdateErpProductCoopParam();
            cp.setBrandName("");
            cp.setBrandModel(Util.parseStringFromMap(itemMap, "SPECS"));    //物料规格型号
            cp.setProductCode(wuliao_prefix + Util.parseStringFromMap(itemMap, "Code"));	//物料编码
            cp.setTitle(Util.parseStringFromMap(itemMap, "Name"));  //物料名称
            cp.setUnit(Util.parseStringFromMap(itemMap, "UnitName"));  //物料单位
            cp.setErpCategoryCode(wuliao_prefix + Util.parseStringFromMap(itemMap, "CategoryCode"));    //料品分类的编码
            cp.setSystemDomainCode("010");  //系统域
            coopParams[i] = cp;
        }
        param.setCoopParams(coopParams);
        AlibabaCaigouProductBatchAddOrUpdateErpProductsResult result = apiExecutor.execute(param, config.token).getResult();
        if(!result.getSuccess()){
            throw new ServiceException("同步1688失败,接口错误[" + result.getMessage() + "]");
        }
        try {
            Thread.sleep(200L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
